package com.example.testone.utils

import android.content.Context

class SizeUtils {
    companion object{

        /**
         * dp 2 px
         *
         * @param dpValue dp
         */
        fun dp2px(context: Context, dpValue: Float): Int {
            val density = context.resources.displayMetrics.density
            return (dpValue * density + 0.5f).toInt()
        }

        /**
         * px 2 dp
         *
         * @param pxValue px
         */
        fun px2dp(context: Context, pxValue: Float): Int {
            val density = context.resources.displayMetrics.density
            return (pxValue / density + 0.5f).toInt()
        }

        /**
         * sp 2 px
         *
         * @param spValue sp
         */
        fun sp2px(context: Context, spValue: Float): Int {
            val scaledDensity = context.resources.displayMetrics.scaledDensity
            return (spValue * scaledDensity + 0.5f).toInt()
        }

        /**
         * px 2 sp
         *
         * @param pxValue px
         */
        fun px2sp(context: Context, pxValue: Float): Int {
            val scaledDensity = context.resources.displayMetrics.scaledDensity
            return (pxValue / scaledDensity + 0.5f).toInt()
        }

        /**
         * The absolute width of the available display size in pixels.
         */
        fun widthPixels(context: Context): Int {
            return context.resources.displayMetrics.widthPixels
        }

        /**
         * The absolute height of the available display size in pixels.
         */
        fun heightPixels(context: Context): Int {
            return context.resources.displayMetrics.heightPixels
        }
    }
}